Skip to content

gh-162054: Use vsnprintf() on all platforms in PyOS_vsnprintf() - #162053

Open
deronnax wants to merge 2 commits into
python:mainfrom
deronnax:mysnprintf-drop-msvc-vsnprintf
Open

deronnax wants to merge 2 commits into
python:mainfrom
deronnax:mysnprintf-drop-msvc-vsnprintf

Conversation

@deronnax

@deronnax deronnax commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Since Visual Studio 2015, the MSVC runtime provides a C99-conforming vsnprintf(), so the _vsnprintf() special case in PyOS_vsnprintf() is no longer needed. On truncation, _vsnprintf() returned a negative value, unlike the C99 behavior documented for PyOS_vsnprintf().

Also adds a test for the truncation case in test_ctypes and updates the outdated comment.

No existing caller relies on the negative return value for truncation. Marked as draft until the Windows CI results are in.

Fixes #162054. See also #101703 (comment)

Since Visual Studio 2015, the MSVC runtime provides a C99-conforming
vsnprintf(), so the _vsnprintf() special case is no longer needed.
Add a test for the truncation case and update the outdated comment.
@deronnax deronnax changed the title Use vsnprintf() on all platforms in PyOS_vsnprintf() gh-162054: Use vsnprintf() on all platforms in PyOS_vsnprintf() Nov 15, 2026
@deronnax
deronnax marked this pull request as ready for review November 15, 2026 15:49
@aisk aisk added the skip news label Nov 15, 2026
@aisk

aisk commented Nov 15, 2026

Copy link
Copy Markdown
Member

According to the comment changes in this PR, the behavior of PyOS_vsnprintf() has changed on Windows, since it is a public C API, I think this is more like a bugfix than a refactor, so a NEWS entry is required.

@aisk aisk removed the skip news label Nov 15, 2026
@deronnax

deronnax commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Ah! This wasn't meant as a bug fix: the intent was dead code unnecessary special implementation elimination, since the MSVC-specific _vsnprintf() branch is unneeded with the UCRT. That being said, I agree it can also be seen as a bugfix, as on Windows the return value on truncation now matches the documented behavior. I've added a NEWS entry.

@picnixz

picnixz commented Nov 15, 2026

Copy link
Copy Markdown
Member

Is it dead code for everyone or are there versions where it is NOT dead code or extension modules that could be affected when running on an older platform? If this is possible, I prefer not to change anything.

@deronnax

deronnax commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

"Dead code" was not the right term, sorry: the branch is really executed on MSVC builds, it is just a special case that is not needed anymore.

Starting from python 3.5, PyOS_vsnprintf() is compiled for Windows using Microsoft Visual C++ 14.0, this Visual Studio uses this new UCRT.

The difference is the return value when the output is truncated: -1 before (windows specific), rv >= size now (standard C99).

I don't think it can break much:

The doc says rv >= size means truncated, and rv < 0 means "failed, the cause depends on the platform". So nobody can officially rely on -1 for truncation.
An extension following the doc handles both cases and keeps working (and is even more right on Windows now).
The only code at risk would be some Windows-only code testing rv == -1 for truncation, or a portable code checking only rv < 0, which already uses silently truncated output on Linux and macOS.

That said, I will not insist much to get this merged. I like this change, it provides a (very) small value in code-readiness/cognitive load, though at the expense of a low risk of breaking some wrongly-written extensions. I know CPython value the second over everything.

@picnixz

picnixz commented Nov 16, 2026

Copy link
Copy Markdown
Member

Let me rephrase it as follows: does the current code rely on fragile details and is it still correct with newer MSVC versions? if so, let's keep it that way since there is a slight behavior change (AFAIU)

@deronnax

Copy link
Copy Markdown
Contributor Author

Current code leaks out an undocumented implementation detail on Windows, but it still correct with newer MSVC.

@picnixz

picnixz commented Nov 16, 2026

Copy link
Copy Markdown
Member

Yeah so we don't need to change it I think. The docs are still correct, namely "something went wrong" so I would lean towards keeping it as is unless Chris thinks it is better to change (I will leave that to him).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use vsnprintf() on all platforms in PyOS_vsnprintf()

4 participants